import React from 'react';
import redux from '../_/redux';
import _ from 'lodash';
import moment from 'moment';
import {Button} from 'antd';
import PatientInfoForm from './patientInfoForm';
import VisitInfoForm from './visitInfoForm';
const loader = (...args) => {
    const {pIBload, vIBload, dataEdit} = args[0];
    if (typeof dataEdit !== "undefined") {
        pIBload({id: dataEdit.pId});
        vIBload({id: dataEdit.Id});
    }
};
let tempSaveVals = [];
@redux('patInfoBasic', {
    prefix: "pIB",
    loader,
    actions: ['create', 'modify', 'load', 'query']
})
@redux('visit', {
    prefix: "vIB",
    loader,
    actions: ['create', 'modify', 'load']
})
export default class _PatientForm extends React.Component {
    state = {
        actionTyp: "none",
        vIBactionTyp: "none"
    };

    componentWillReceiveProps(props) {
        const {dataEdit: data} = props;
        const {dataEdit: rawdata} = this.props;
        if (data !== rawdata && typeof data !== "undefined") {
            loader(props);
        } else if (props.reset) {
            this.setState({
                actionTyp: "reset",
                vIBactionTyp: "reset"
            });
            if (typeof props.doneReset === 'function' && props.reset) {
                props.doneReset();
            }
        }
    }

    dovIBAction = (vals) => {
        tempSaveVals = vals;
        this.setState({vIBactionTyp: "vIBsave"});
    };
    doAction = (typ, datatyp, vals) => {
        switch (typ) {
        case "save":
            switch (datatyp) {
            case "pIB":
                this.dovIBAction(vals);
                break;
            case "vIB":
                tempSaveVals = _.concat(tempSaveVals, vals);
                tempSaveVals[1].patientId = tempSaveVals[0].PatientId;
                tempSaveVals[0].Birthday = tempSaveVals[0].Birthday.format('YYYY-MM-DD');
                tempSaveVals[1].visitDate = tempSaveVals[1].visitDate.format('YYYY-MM-DD');
                if (typeof tempSaveVals[1].Id !== "undefined" && tempSaveVals[1].Id !== "") {
                    this.props.pIBmodify(tempSaveVals[0], this.props);
                    this.props.vIBmodify(tempSaveVals[1], this.props);
                } else {
                    this.props.pIBcreate(tempSaveVals[0], this.props);
                    this.props.vIBcreate(tempSaveVals[1], this.props);
                }
                tempSaveVals[0].Birthday = moment(tempSaveVals[0].Birthday);
                tempSaveVals[1].visitDate = moment(tempSaveVals[1].visitDate);
                tempSaveVals = [];
                this.setState({vIBactionTyp: "none"});
                if (typeof this.props.hideEditor === 'function') {
                    this.props.hideEditor();
                }
                break;
            }
            break;
        case "err":
            switch (datatyp) {
            case "pIB":
                this.setState({actionTyp: "none"});
                break;
            case "vIB":
                this.setState({vIBactionTyp: "none"});
                break;
            }
            break;
        case "reset":
            switch (datatyp) {
            case "pIB":
                this.setState({actionTyp: "none"});
                break;
            case "vIB":
                this.setState({vIBactionTyp: "none"});
                break;
            }
            break;
        }
    };

    handleSubmit(e) {
        e.preventDefault();
        this.setState({actionTyp: "pIBsave"});
    }

    handleReset(e) {
        e.preventDefault();
        this.setState({actionTyp: "reset", vIBactionTyp: "reset" });
    }
    checkPatientInfoById = (val) => {
        this.props.pIBquery({PatientId: val});
    };

    render() {
        return (
            <div>
                <PatientInfoForm pdata={typeof this.props.pIBdata !== "undefined" ? this.props.pIBdata[0] : ""} doAction={this.doAction} actionTyp={this.state.actionTyp} checkPatientInfoById={this.checkPatientInfoById}/>
                <VisitInfoForm vdata={typeof this.props.vIBdata !== "undefined" ? this.props.vIBdata[0] : ""} doAction={this.doAction} vIBactionTyp={this.state.vIBactionTyp}/>
                <div
                    id="footerWrap"
                >
                    <div
                        id="formBottom"
                    >
                        <Button type="primary" onClick={this.handleSubmit.bind(this)}>保存</Button>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <Button
                            type="light"
                            onClick={this.handleReset.bind(this)}
                        >
                            重置
                        </Button>
                    </div>
                </div>
            </div>
        );
    }
}
